package com.itboye.textileexpres.top;

import java.util.List;

import com.itboye.textileexpes.beans.Category_TypeTwo;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.top.GoodsExpandlistAdapter.ViewHolder;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyExpandableListAdapter implements ExpandableListAdapter {
	Context context;
	private List<Category_TypeTwo> armTypes;;
	OnClickListener totopclicker;

	public MyExpandableListAdapter(Context context,
			List<Category_TypeTwo> armTypes, OnClickListener totopclicker,
			List<List<Category_TypeTwo>> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.armTypes = armTypes;
		this.totopclicker = totopclicker;
	}

	public MyExpandableListAdapter(Context context,
			List<Category_TypeTwo> armTypes, OnClickListener totopclicker) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.armTypes = armTypes;
		this.totopclicker = totopclicker;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public int getGroupCount() {
		return armTypes.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return armTypes.get(groupPosition).getChildren().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return armTypes.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return armTypes.get(groupPosition).getChildren().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		return getTextView(armTypes.get(groupPosition).getName());

	}

	class ViewHolder {
		TextView type_tv;
		ImageView to_top;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = (LinearLayout) inflater.inflate(
					R.layout.item_goodsfragmenta_dapter, null);
			holder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
			holder.to_top = (ImageView) convertView.findViewById(R.id.to_top);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.type_tv.setText(armTypes.get(groupPosition).getChildren()
				.get(childPosition).getName());
		holder.to_top.setOnClickListener(totopclicker);
		holder.to_top.setTag(childPosition);
		holder.to_top.setVisibility(View.GONE);
		return convertView;

	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {

	}

	@Override
	public void onGroupCollapsed(int groupPosition) {

	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		return 0;
	}

	private TextView getTextView(String text) {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setText(text);
		textView.setPadding(10, 10, 0, 10);
		return textView;
	}
}
